<template>
	<view class="pd16_15">
			<navigator url="/pages/client/member/addressform">
				<view class="box noshadow pd10_15 flex alcenter center" :style="{color:tempColor}">
					<text class="iconfont iconbtn_add_liner ft20 mr5"></text>
					<text class="ft16 ftw600">新增地址</text>
				</view>
			</navigator>	
			
			<radio-group @change="changeDefaultAddr">
				<view v-for="(item,index) in addrs" :key="index" class="box noshadow pd16_15 mt16">
					<view class="ft14 cl-main ftw600">{{item.name}}  {{item.tel}}</view>
					<view class="mt8 ft12 cl-notice">{{item.province}}-{{item.city}}-{{item.district}}-{{item.address}}</view>
					<view class="bd-line mt20 mb16"></view>
					<view class="flex alcenter space">
						<view @click="setaddup(item.id,index)" class="flex alcenter">
							<radio :checked="item.is_default == 1"  :color="tempColor"  />
							<text class="ml10 ft14 cl-info2">默认地址</text>
						</view>
						<view class="flex alcenter">
							<text @click="del(item.id,index)" class="iconfont iconbtn_delete mr60 ft20 cl-info2"></text>
							<text @click="edit(item)" class="iconfont iconbtn_edit ft20 cl-info2"></text>
						</view>
					</view>
				</view>
			
			</radio-group>
			<uni-load-more :status="status" :content-text="contentText" />
	</view>
</template>

<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	  export default {
		  components: {
		  	uniLoadMore
		  },
		data(){
			return {
				addrs:[],
				listData: [],
				last_id: 0,
				reload: true,
				status: 'more',
				statusa: false,
				text:'',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有数据了'
				}
			}
		},
		onLoad(){
			this.getdata()
		},
		onShow() {
			this.last_id=0
			this.reload=true
			this.getList();
		},
		onReachBottom() {
			this.status = 'more';
			this.getList();
		},
		//监听搜索框文本变化
		onNavigationBarSearchInputChanged(e) {
			let text = e.text;
			if (text) {
				this.text=text;	
				return;
			}
		},
		//监听点击搜索按钮事件
		onNavigationBarSearchInputConfirmed(e) {
			//this.hide();
			let text = e.text;
			let this_=this
			this.text=text;
			this.last_id=0
			this.reload= true
			if (!text) {
				uni.showModal({
					title: '',
					content:"请输入搜索内容",
					showCancel: false,
					confirmText: "确定",
					confirmColor:"#e19503",
					success: function (res) {
						// #ifdef APP-PLUS
						plus.key.hideSoftKeybord();
						// #endif
						this_.text=''
						this_.getList();//点击搜索方法
					}
				});
				return;
			} else {
				this.getList();//点击搜索方法
			}
			// #ifdef APP-PLUS
			plus.key.hideSoftKeybord();
			// #endif
			
		},
		// 监听原生标题导航按钮点击事件
		onNavigationBarButtonTap(e) {
			console.log(e.index)
				// #ifdef APP-PLUS
				plus.key.hideSoftKeybord();
				// #endif
				if(e.index==0){
					this.text='';
					this.last_id=0
					this.reload= true
					this.getList();
				}
		},
		methods:{
			getList() {
				let data = {
					//column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
				};
				var limit=10;
				if (this.last_id>0) {
					//说明已有数据，目前处于上拉加载
					this.status = 'loading';
					data.offset = this.last_id*limit;
					data._ = new Date().getTime() + '';
				}
				
				if(this.text){
					data.text=this.text
				}
				data.limit=limit		
				data.token = uni.getStorageSync("userinfo").token;
				uni.request({
					url: this.configs.webUrl+'/api/user/addlist',
					data: data,
					success: data => {
						console.log(data.data)
						if (data.data.total>0) {
							let list = data.data.rows;
							this.addrs = this.reload ? list : this.addrs.concat(list);
							this.reload = false;
							this.last_id = this.last_id+1;
							if(data.data.total<this.last_id*limit){
								this.status = '';
							}
						}else{
							this.contentText.contentdown='没有数据'
						}
					},
					fail: (data, code) => {
						//console.log('fail' + JSON.stringify(data));
					}
				});
			},
			getdata(){
				//http://v.demo.hongcd.com/assets/region.json
				let data = {};
				let this_ = this;
				uni.request({
					url: this.configs.webUrl+'/api/shop/jsonaa',
					data: data,
					dataType: 'json',
					success: data => {
						//console.log(data.data)
						uni.setStorage({//缓存配置信息
							key: 'AddData',  
							data: JSON.parse(data.data)
						})
					},
					fail: (data, code) => {
					}
				});
			},
			changeDefaultAddr(e){
				let id = parseInt(e.detail.value);
				for(var a in this.addrs){
					this.addrs[a].is_default = this.addrs[a].address_id == id ? 1 : 0;
				}
			},
			setaddup(id,index){
				
				let data = {};
				data.id=id
				data.token = uni.getStorageSync("userinfo").token;
				data.uid = uni.getStorageSync("userinfo").id;
				let this_ = this;
				uni.request({
					url: this.configs.webUrl+'/api/user/setaddup',
					data: data,
					dataType: 'json',
					success: data => {
						this.addrs=[]
						uni.showToast({ title: data.data.msg,icon:"none" });
						this.last_id=0
						this.reload=true
						this.getList();
					},
					fail: (data, code) => {
					}
				});
			},
			
			del(id,index){
				let this_ = this;
				uni.showModal({
					title: '温馨提示',
					content: '确认要删除？',
					showCancel: true,
					confirmText: "确定",
					success: function (res) {
						if (res.confirm) {
							this_.dels(id,index)
						} else if (res.cancel) {
							
						}
					}
				});
			},
			
			dels(id,index){
				
				let data = {};
				data.id=id
				data.token = uni.getStorageSync("userinfo").token;
				data.uid = uni.getStorageSync("userinfo").id;
				let this_ = this;
				uni.request({
					url: this.configs.webUrl+'/api/user/adddel',
					data: data,
					dataType: 'json',
					success: data => {
						uni.showToast({ title: data.data.msg,icon:"none" });
						this_.addrs.splice(index, 1);
					},
					fail: (data, code) => {
					}
				});
			},
			edit(item){
				
				uni.setStorage({//缓存配置信息
					key: 'AdditemData',  
					data: item
				})
				uni.navigateTo({
					url:'/pages/client/member/addresseditform'
				})
			}
		}
	}
</script>

<style>
	
</style>